<template>
    <el-card>
        <el-form :model="queryParams" ref="queryParams" :inline="true" label-width="100px">
            <el-form-item label="服务名：" prop="name">
                <el-input v-model="queryParams.name" placeholder="服务名" size="small"></el-input>
            </el-form-item>
            <el-form-item label="服务描述：" prop="title">
                <el-input v-model="queryParams.title" placeholder="服务描述" size="small"></el-input>
            </el-form-item>
             <el-form-item label="团队：" prop="team">
                <el-input v-model="queryParams.team" placeholder="团队" size="small"></el-input>
            </el-form-item>
            <el-form-item label="负责人：" prop="personInCharge">
                <el-input v-model="queryParams.personInCharge" placeholder="负责人" size="small"></el-input>
            </el-form-item>
            <el-form-item >
                <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-delete" size="small" @click="resetQuery('queryParams')">清空</el-button>
            </el-form-item>
        </el-form>
        <el-row :gutter="10">
            <el-col :span="24">
                <div style="margin-bottom: 10px">
                    <el-button type="primary" icon="el-icon-plus" size="small" @click="handleAdd">新增</el-button>
                    <el-button type="danger" icon="el-icon-delete" plain size="small" @click="handleDelete">删除</el-button>
                </div>
            </el-col>
        </el-row>
        <el-table :data="tableData" border style="width: 100%" size="small">
            <el-table-column type="index" label="#" width="50"></el-table-column>
            <el-table-column label="服务名" prop="name"></el-table-column>
            <el-table-column label="服务描述" prop="title"></el-table-column>
            <el-table-column label="团队" prop="team"></el-table-column>
            <el-table-column label="负责人" prop="personInCharge"></el-table-column>
            <el-table-column label="备注" prop="description"></el-table-column>
            <el-table-column label="操作" width="300" align="center">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">编辑</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row.id)">删除</el-button>
                    <el-button size="mini" type="text" icon="el-icon-setting" @click="handlePower(scope.row)">权限</el-button>
                </template>
            </el-table-column>
        </el-table>
         <el-pagination
                v-show="total>0"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                background
                :current-page="1"
                :page-sizes="[10, 20, 30, 40,50,100]"
                :page-size="10"
                layout="total, sizes, prev, pager, next, jumper"
                class="pagination-container"
                :total="total">
        </el-pagination>
        <el-dialog
            :title="formTitle"
            :visible.sync="dialogVisible"
            width="40%"
            >
            <el-form :model="form" ref="form" :rules="rules">
                <el-row :gutter="10">
                  <el-col :span="12">
                    <el-form-item label="服务名" prop="name" required label-width="80px">
                        <el-input v-model="form.name" placeholder="请输入服务名，如：user-service" size="small"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="服务描述" prop="title" required label-width="120px">
                        <el-input v-model="form.title" placeholder="请输入服务描述，如：用户服务" size="small"></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
                 <el-row :gutter="10">
                  <el-col :span="12">
                    <el-form-item label="团队" prop="team" required label-width="80px">
                        <el-input v-model="form.team" placeholder="请输入团队" size="small"></el-input>
                    </el-form-item>
                  </el-col>
                   <el-col :span="12">
                    <el-form-item label="负责人" prop="personInCharge" required label-width="80px">
                        <el-input v-model="form.personInCharge" placeholder="请输入负责人" size="small"></el-input>
                    </el-form-item>
                  </el-col>
                 </el-row>
                 <el-row>
                   <el-col :span="24">
                    <el-form-item label="备注" prop="description" label-width="80px">
                        <el-input v-model="form.description" size="small" rows="6" type="textarea"></el-input>
                    </el-form-item>
                   </el-col>
                 </el-row>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false" size="small">取 消</el-button>
                <el-button type="primary" @click="handleSave('form')" size="small">确 定</el-button>
            </span>
        </el-dialog>
        <el-dialog :title="`【${serviceName}】用户权限配置`" :visible.sync="powerDialog" width="20%">
            <el-tree
                :data="treeData"
                node-key="id"
                :props="defaultProps"
                :default-expanded-keys="['0']"
                show-checkbox>
            </el-tree>
            <span slot="footer" class="dialog-footer">
                <el-button @click="powerDialog = false" size="small">取 消</el-button>
                <el-button type="primary" @click="handlePower" size="small">确 定</el-button>
            </span>
        </el-dialog>
    </el-card>
</template>

<script>
import { saveServiceData,getServiceList,getServiceTree,updateService,remove } from '@/api/aggregate/service.api.js'
    export default {
        data() {
            return {
                queryParams: {
                    current: 1,
                    size: 10,
                    name: undefined,
                    title: undefined,
                    team: undefined,
                    personInCharge: undefined
                },
                total: 0,
                formLabelWidth: '120px',
                tableData: [],
                dialogVisible: false,
                form: {},
                rules: {
                    name: [
                        { required: true, message: '请输入服务名', trigger: 'blur' },
                    ],
                    title: [
                        { required: true, message: '请输入服务描述', trigger: 'blur' },
                    ],
                    team: [
                        { required: true, message: '请输入团队', trigger: 'blur' },
                    ],
                    personInCharge: [
                        { required: true, message: '请输入负责人', trigger: 'blur' },
                    ],
                },
                powerDialog: false,
                 props: {
                    label: 'name',
                 },
                 treeData: [
                    {id: '0',label: '全选',children: []}
                 ],
                 formTitle: '新增',
                  defaultProps: {
                    children: 'children',
                    label: 'label'
                 },
                 serviceName: ''
            }
        },
        mounted() {
           this.getList();
        },
        methods: {
              async getList() {
                const result = await getServiceList(this.queryParams);
                if(result.code === 200) {
                    this.tableData = result.data.records;
                    this.total = result.data.total
                }
            },
              // 搜索
            handleQuery() {
                this.queryParams.current = 1;
                this.getList();
            },
            // 清空
            resetQuery(formName) {
                this.$refs[formName].resetFields();
                this.getList();
            },
            handleSizeChange(val) {
                this.queryParams.size = val;
                this.getList();
            },
            handleCurrentChange(val) {
                this.queryParams.current = val;
                this.getList();
            },
            handleAdd() {
                this.dialogVisible = true;
            },
            handleDelete(id) {
                this.$confirm('确定将选择的数据删除?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(async () => {
                    const result = await remove(id);
                    if(result.code === 200) {
                            this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        this.getList();
                    }else {
                        this.$message.error(result.data.msg);
                    }
                }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });          
            });
            },
            handleEdit(row) {
                // 深拷贝赋值 避免赋值影响表格
                this.form = JSON.parse(JSON.stringify(row))
                this.formTitle = '编辑';
                this.dialogVisible = true;
            },
            /**
             * 保存
             */
            handleSave(formName) {
               this.$refs[formName].validate(async (valid) => {
                 if (valid) {
                    if(this.form.id) {
                        const result = await updateService(this.form);
                         if(result.code===200) {
                            this.$message({
                                    message: '修改成功',
                                    type: 'success'
                            });
                         }
                    }else{
                         const result = await saveServiceData(this.form);
                         if(result.code===200) {
                            this.$message({
                                    message: '保存成功',
                                    type: 'success'
                                });
                        }
                    }
                        this.dialogVisible = false;
                        this.getList();
                 } else {
                    console.log('error submit!!');
                    return false;
                 }
                });
            },
            /**
             * 权限设置
             */
            async handlePower(row) {
                this.powerDialog=true;
                this.serviceName = row.name
                const result = await getServiceTree(row.id);
                if(result.code===200) {
                     let data =[];
                     result.data.forEach(item=>{
                        let obj = {id:item.userId,label:item.userName };
                        data.push(obj);
                    })
                    this.treeData[0].children = data;
                }
            },
        }
    }
</script>
<style lang="scss" scoped>
.pagination-container {
    padding: 10px;
    text-align: right;
}
</style>